<div class="layui-fluid">
	<div class="layui-row">
		<div class="layui-col-xs12">
			<form class="layui-form" action="" lay-filter="teacheraddfilter">
				<div class="playtab">
					<div class="layui-card-header">基本信息</div>
			<br>
					<input type="hidden" name="tbId" />
					<input type="hidden" name="tbType" value="1" />
					<input type="hidden" name="type" value="1" />
					<div class="layui-form-item">
						<label class="layui-form-label">讲师头像 <span class="reddot">*</span></label>
						<div class="layui-input-inline">
							<input type="hidden" name="photo" id="upheadid" />
							<div class="layui-upload-drag" id="upheadimgfile">
								<i class="layui-icon"></i>
								<p>点击上传图片</p>
							</div>
						</div>
					</div>
					<input type="hidden" name="tbId">
					<div class="layui-form-item">
						<label class="layui-form-label">真实姓名 <span class="reddot">*</span></label>
						<div class="layui-input-inline">
							<input type="text" name="tbName" lay-verify="username" placeholder="请输入讲师真实姓名" autocomplete="off" value=""
							 class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">证件号码</label>
						<div class="layui-input-inline">
							<!-- lay-verify="identity" -->
							<input type="text" name="numbers" placeholder="请输入身份证号码" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">讲师简介</label>
						<div class="layui-input-inline">
							<textarea name="introduction" class="layui-textarea"></textarea>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">讲师类型</label>
						<div class="layui-input-inline">
							<div class="bqs " id="bqid">

							</div>
							<!-- <input type="text" name="tbTitle" lay-verify="username" placeholder="请输入自定义课程类型,并回车键进行添加" autocomplete="off" value="" class="layui-input"> -->
							<div class="bqs " id="bqid2">

							</div>
						</div>
					</div>
<!-- 
					<div class="layui-form-item">
						<label class="layui-form-label">讲师类型</label>
						<div class="layui-input-inline">
							<div class="sourcebox layui-icon " id="typebox">
							</div>
							<select name="addIdsstring" lay-filter="teachertypefilter">
								<option value=" ">请选择讲师类型</option>
							</select>
						</div>
					</div> -->

					<div class="layui-form-item">
						<label class="layui-form-label">讲师性别</label>
						<div class="layui-input-block">
							<input type="radio" name="sex" value="1" title="男" checked>
							<input type="radio" name="sex" value="2" title="女">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">讲师来源 <span class="reddot">*</span></label>
						<div class="layui-input-inline">
							<select name="tbSourceId" lay-filter="tbSourcefilter">
								<option value=" ">请选择讲师来源</option>
							</select>
						</div>
					</div>


				</div>
				<div class="playtab">

					<div class="layui-card-header">帐号信息</div>
					<br>
					<div class="layui-form-item">
						<label class="layui-form-label">手机号码 </label>
						<div class="layui-input-inline">
							<input type="text" name="phone" placeholder="请输入手机号码"  autocomplete="off" value=""
							 class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">后台密码</label>
						<div class="layui-input-inline">
							<input type="text" name="password" placeholder="请输入后台密码" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
							<div class="layui-btn layui-btn-primary" id="returnqx">取消</div>
						</div>
					</div>

				</div>

		</div>
		</form>
	</div>
</div>
</div>
<!-- 脚本 -->
<script>
	layui.use(['form', 'layedit', 'upload', 'laytpl'], function() {
		var form = layui.form,
			$ = layui.jquery,
			upload = layui.upload,
			laytpl = layui.laytpl,
			layedit = layui.layedit;
		form.render();
		// var richdemo = layedit.build('tbIntroductionid'); //讲师简介
		var router = layui.router(); //获取路由

		upload.render({
			elem: '#upheadimgfile',
			url: FileUp +'?fileType=1',
			acceptMime: 'image/*',
			size: '800',
			accept: 'images',
			data: {
				fileType: 1
			},
			done: function(res) {
				console.log(res)
				if (res.status == 200) {
					$("#upheadid").val(res.data.filePath);
					$("#upheadimgfile").html('	<img src="' + IMG_URL + res.data.filePath + '" >');
					layer.msg("上传成功", {
						"icon": 1
					});
				}
			},
			error: function() {
				//请求异常回调
			}
		});



		///获取课程类型
		videotypelist = []
		POSTAJAX(VtypeGet, {
			currentPage: 1,
			pageSize: 1000,
			tbName: ''
		}, function(res) {

			$.each(res.data.data, function(idx, obj) {
			obj.ischoose = 2;
			obj.ishave = 2;
				$("#bqid").append('<div class="bq bq2 cupo" id="bq' + obj.tbId + '" data.id="' + obj.tbId +
					'" onclick="videotypechoosefn(' + idx + ')">' + obj.tbProdustsTypeName +
					'</div>');
				videotypelist.push(obj)
			})

		})

		var sourcetypejson = [],
			newsourcejson = []; // 类型json
		POSTAJAX(VtypeGet, {
			currentPage: 1,
			pageSize: 1000,
			tbName: ''
		}, function(res) {
			$.each(res.data.data, function(idx, obj) {
				$("select[name='addIdsstring']").append('<option value="' + obj.tbId + '">' + obj.tbProdustsTypeName +
					'</option>');
			})
			form.render();
			form.on('select(teachertypefilter)', function(data) {

				sourcetypejson.push(data.value);
				console.log(sourcetypejson)
				var $index = data.elem.selectedIndex;
				if ($index != 0) {
					$("#typebox").append('<div class="asource " data-id="' + data.value + '">	' + data.elem[data.elem.selectedIndex]
						.text +
						' &#x1006;	</div>');
					$(".asource").on("click", function() {
						$(this).remove();
						delete sourcetypejson[$(this).index()];
					})
				}
			})
		})


		// 获取来源
		POSTAJAX(hhSourceGet, {
			currentPage:1,
			pageSize:100

		}, function(res) {
			$.each(res.data.data, function(idx, obj) {
				$("select[name='tbSourceId']").append('<option value="' + obj.tbId + '">' + obj.tbSourceName + '</option>');
				
			})
			form.render();
			form.on('select(tbSourcefilter)', function(data) {
				//监听选择省份
				// $("select[name='tbSourceId']").html('');
				// console.log(data.value)
				// POSTAJAX(CityGet, {
				// 	pid: data.value
				// }, function(res) {
				// 	$.each(res.data, function(idx, obj) {
				// 		$("select[name='tbCityId']").append('<option value="' + obj.id + '">' + obj.name + '</option>')
				// 		form.render();
				// 	})
				// })
			});
		})
		oldtypelist = [];
		if (router.search.tbId) {
			// 有id的传参的值  查询 id
			POSTAJAX(LecturerLPById, {
				tbId: router.search.tbId
			}, function(res) {
					setTimeout(function() {
				form.val("teacheraddfilter", {
					"introduction": res.data.tbIntroduction,
					"numbers": res.data.tbIdCard,
					"password": res.data.password,
					"phone": res.data.phone,
					"photo": res.data.photo,
					"sex": res.data.sex,
					"tbName": res.data.realName,
					"tbSourceId": res.data.tbSourceId,
					"tbId": res.data.tbId,
					"type": res.data.type,
				})
				
				$.each(res.data.produstsTypes, function(idx, obj) {
					$.each(videotypelist , function(idx1 , obj1){
						console.log(idx)
						if(obj.tbId == obj1.tbId){
							videotypelist[idx1].ishave = 1 ;
							videotypechoosefn(idx1) ;
								console.log(JSON.stringify(videotypelist) )
						}
					})
				})
}, 500);


				$("#upheadimgfile").html('	<img src="' + IMG_URL + res.data.photo + '" >');

				// layedit.setContent(richdemo, res.data.tbIntroduction);

			})

			//修改
			form.on('submit(formDemo)', function(data) {
				var tpostjson = data.field;
				tpostjson.phone =  '' ;
				tpostjson.ids = []; //视频类型初始化
				tpostjson.dels = [];
				$.each(videotypelist, function(idx, obj) {
					if(obj.ischoose == 1){
						//选中的
						if(obj.ishave == 1 ){
							
						}else {
								tpostjson.ids.push(obj.tbId)
						}
					}else{
						///未选中的
						if(obj.ishave == 1 ){
								tpostjson.dels.push(obj.tbId)
						}else {
							
						}
					}
				})
				setTimeout(function() {
				POSTAJAX(LecturerMod, tpostjson, function(res) {
					if (res.status == 200) {
						
						layer.msg("修改成功", {
							"icon": 1
						})
						window.location.href = '#/teacher/homepage?tbId='+router.search.tbId;
					} else {
						layer.msg(res.msg, {
							"icon": 2
						})
					}
				})
				}, 1000)
				return false;
			});
		} else {
			// 新增
			//监听提交
			form.on('submit(formDemo)', function(data) {
				var tpostjson = data.field;
					tpostjson.ids = [] ; 
				$.each(videotypelist, function(idx, obj) {
					if (obj.ischoose == 1) {
						tpostjson.ids.push(obj.tbId)
					}
				})
				POSTAJAX(LecturerAdd, tpostjson, function(res) {
					if (res.status == 200) {
						layer.msg("新增成功", {
							"icon": 1
						})
						window.location.href = '#/teacher/list' ; 
					} else {
						layer.msg(res.msg, {
							"icon": 2
						})
					}
				})
				// }
				return false;
			});

		}
		$("#returnqx").on("click", function() {
			location.href = '#/teacher/list';

		})


	});

	function videotypechoosefn(idx) {
		//视频类型选择函数
		// console.log(idx)
		if (videotypelist[idx].ischoose == 2) {
			videotypelist[idx].ischoose = 1;
			$(".bq").eq(idx).addClass('bqactive')
		} else {
			$(".bq").eq(idx).removeClass('bqactive')
			videotypelist[idx].ischoose = 2;
		}


	}
</script>
<!-- 样式 -->
<style scoped>




</style>
